package com.seeyon.apps.kk.util;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.vo.jcom.lang.StringUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.apps.kk.manager.FormDataBaseManager;
import com.seeyon.apps.kk.manager.QiangsMemberManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.content.comment.CommentManager;
import com.seeyon.ctp.common.detaillog.manager.DetaillogManager;
import com.seeyon.ctp.common.detaillog.vo.FlowNodeDetailAffairVO;
import com.seeyon.ctp.common.detaillog.vo.ProcessLogVO;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.i18n.ResourceUtil;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.common.po.comment.CtpCommentAll;
import com.seeyon.ctp.common.processlog.dao.ProcessLogDAO;
import com.seeyon.ctp.common.processlog.po.ProcessLog;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.DateUtil;

public class CollaborationEventThread {
	
	private static final Log log = LogFactory.getLog(CollaborationEventThread.class);
	
	
	private String formName;
	private CtpAffair affair;
	private ColSummary summary;
    private CtpCommentAll comment;
    private FlowNodeDetailAffairVO nodeDetail;
    
    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private ProcessLogDAO processLogDAO = (ProcessLogDAO) AppContext.getBean("processLogDAO");
    private CommentManager ctpCommentManager = (CommentManager)AppContext.getBean("ctpCommentManager");
	private DetaillogManager detaillogManager = (DetaillogManager)AppContext.getBean("detaillogManager");
	private FormDataBaseManager baseFormManager = (FormDataBaseManager) AppContext.getBean("kkBaseFormManager");
	private QiangsMemberManager baseMemberManager = (QiangsMemberManager) AppContext.getBean("memberQiangsManager");
	
    public CollaborationEventThread(final String formName, final CtpAffair affair, final FlowNodeDetailAffairVO nodeDetail){
    	super();
    	this.formName = formName;
    	this.affair = affair;
    	this.nodeDetail = nodeDetail;
    	Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Map<String, Object> param = new HashMap<String, Object>();
					if(null != nodeDetail.getCompleteTime()){
						param.put("操作时间", nodeDetail.getCompleteTime());
					} else {
						param.put("操作时间", DateUtil.currentDate());
					}
					Thread.sleep(30*1000);
					
					CtpCommentAll comment = null;
					List<CtpCommentAll> commentList = ctpCommentManager.findCommentsByAffairId(affair.getId());
					if(CollectionUtils.isNotEmpty(commentList)){
						comment = commentList.get(0);
						comment.setContent("");
						comment.setExtAtt1("取回");
					} else {
						comment = new CtpCommentAll();
						comment.setCreateId(affair.getMemberId());
						comment.setExtAtt1("发起");
						comment.setId(-1L);
					}
					param.put("表单名", formName);
					param.put("审批意见", comment.getContent());
					param.put("操作人", comment.getCreateId());
					
					V3xOrgMember  member = orgManager.getMemberById(comment.getCreateId());
					param.put("操作人wwid", member.getCode());
					
					if(!StringUtil.isEmpty(comment.getExtAtt1())){
						param.put("动作内容", ResourceUtil.getString(comment.getExtAtt1()));
					} else if(!StringUtil.isEmpty(comment.getExtAtt3())){
						param.put("动作内容", ResourceUtil.getString(comment.getExtAtt3()));
					} else {
						param.put("动作内容", ResourceUtil.getString(comment.getExtAtt2()));
					}
					param.put("流程ID", nodeDetail.getObjectId());
					param.put("表单标题", nodeDetail.getSubject());
					param.put("流程节点名", nodeDetail.getPolicyName().replace("node.policy.collaboration","协同"));
					param.put("审批状态", nodeDetail.getStateLabel().replace("collaboration.state.14.done", "已办"));
					if(StringUtil.isEmpty(nodeDetail.getDealTime())
							|| "无".equals(nodeDetail.getDealTime())){
						String deal = baseMemberManager.convertDealTime(nodeDetail.getReceiveTime(), (Date)param.get("操作时间"), member.getOrgAccountId());
						param.put("有效处理时长", deal);
					} else {
						param.put("有效处理时长", nodeDetail.getDealTime());
					}
					param.put("接收时间", nodeDetail.getCreateDate());
					if(null != nodeDetail.getFirstViewDate()){
						param.put("首次查看时间", nodeDetail.getFirstViewDate());
					}
					List<ProcessLogVO> processLogList = detaillogManager.getProcessLog(nodeDetail.getProcessId());
					
					for(int i = processLogList.size() - 1; i >= 0; i--) {
						ProcessLogVO vo = processLogList.get(i);
						ProcessLog process = processLogDAO.get(Long.valueOf(vo.getId()));
						if(comment.getId().equals(process.getCommentId())) {
							if(!"取回".equals(comment.getExtAtt1())){
								param.put("操作说明", vo.getDesc().replace("(collaboration)", "(协同)"));
							}
							break;
						}
					}
					baseFormManager.saveUnflowData(Constants.nodeFormCode, Constants.createUser, param);
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					log.error("##强生客开异常", e);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					log.error("##强生客开异常", e);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					log.error("##强生客开异常", e);
				}
			}
    		
    	});
    	t.start();
    }
	public CollaborationEventThread(final String formName, final CtpCommentAll comment, final FlowNodeDetailAffairVO nodeDetail){
		super();
    	this.formName = formName;
		this.comment = comment;
		this.nodeDetail = nodeDetail;
		
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Map<String, Object> param = new HashMap<String, Object>();
					if(null != nodeDetail.getCompleteTime()){
						param.put("操作时间", nodeDetail.getCompleteTime());
					} else {
						param.put("操作时间", DateUtil.currentDate());
					}
					Thread.sleep(30*1000);

					param.put("表单名", formName);
					param.put("操作人", comment.getCreateId());
					
					V3xOrgMember member = orgManager.getMemberById(comment.getCreateId());
					param.put("操作人wwid", member.getCode());
					
					param.put("流程ID", nodeDetail.getObjectId());
					param.put("表单标题", nodeDetail.getSubject());
					param.put("流程节点名", nodeDetail.getPolicyName().replace("node.policy.collaboration","协同"));
					param.put("审批状态", nodeDetail.getStateLabel().replace("collaboration.state.14.done", "已办"));
					if(StringUtil.isEmpty(nodeDetail.getDealTime())
							|| "无".equals(nodeDetail.getDealTime())){
						String deal = baseMemberManager.convertDealTime(nodeDetail.getReceiveTime(), (Date)param.get("操作时间"), member.getOrgAccountId());
						param.put("有效处理时长", deal);
					} else {
						param.put("有效处理时长", nodeDetail.getDealTime());
					}
					param.put("接收时间", nodeDetail.getReceiveTime());
					if(!"结束".equals(nodeDetail.getStateLabel())){
						param.put("审批意见", comment.getContent());
						if(!StringUtil.isEmpty(comment.getExtAtt1())){
							param.put("动作内容", ResourceUtil.getString(comment.getExtAtt1()));
						} else if(!StringUtil.isEmpty(comment.getExtAtt2())){
							param.put("动作内容", ResourceUtil.getString(comment.getExtAtt2()));
						} else if(!StringUtil.isEmpty(comment.getExtAtt3())){
							param.put("动作内容", ResourceUtil.getString(comment.getExtAtt3()));
						} else {
							param.put("动作内容", "提交");
						}
						;
						if(null != nodeDetail.getFirstViewDate()){
							param.put("首次查看时间", nodeDetail.getFirstViewDate());
						}
					} else {
						param.put("动作内容", "结束");
					}
					List<ProcessLogVO> processLogList = detaillogManager.getProcessLog(nodeDetail.getProcessId());
					
					boolean isContinue = false;
					for(int i = processLogList.size() - 1; i >= 0; i--) {
						ProcessLogVO vo = processLogList.get(i);
						Long processId = Long.valueOf(vo.getId());
						ProcessLog process = (ProcessLog)processLogDAO.get(processId);
						if(comment.getId().equals(process.getCommentId())){
							param.put("操作说明", vo.getDesc().replace("(collaboration)", "(协同)"));
							baseFormManager.saveUnflowData(Constants.nodeFormCode, Constants.createUser, param);
							isContinue = true;
							break;
						}
					}
					if(!isContinue){
						baseFormManager.saveUnflowData(Constants.nodeFormCode, Constants.createUser, param);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					log.error("##强生客开异常", e);
				}
				
			}
		});
		t.start();
	}
	
}
